#include "registerdialog.h"
#include "cryptstring.h"
#include "tools.h"
#include <QPushButton>
#include <QLineEdit>
#include <QTcpSocket>

#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFont>
#include <QMessageBox>

RegisterDialog::RegisterDialog(MyTcpSocket * socket, QWidget *parent):
    QDialog(parent),
    m_clientTcpSocket(socket)
{
    //setParent(parent);

    this->setWindowTitle("智慧监控系统注册面");
    setMaximumSize(400,300);
    setMinimumSize(400,300);



    QPushButton *button_longin = new QPushButton();
    button_longin->setMinimumSize(80,40);
    button_longin->setMaximumWidth(100);
    button_longin->setText("注册");

    QPushButton *button_cancel = new QPushButton();
    button_cancel->setMinimumSize(80,40);
    button_cancel->setMaximumWidth(100);
    button_cancel->setText("取消");


    QFont font;
    font.setFamily("等线");

    font.setPointSize(15);
    lineEdit_username = new QLineEdit();
    lineEdit_username->setMinimumHeight(30);
    lineEdit_username->setMaximumWidth(300);
    lineEdit_username->setAlignment(Qt::AlignHCenter);
    lineEdit_username->setFont(font);

    lineEdit_passsword = new QLineEdit();
    lineEdit_passsword->setMinimumHeight(30);
    lineEdit_passsword->setMaximumWidth(300);
    lineEdit_passsword->setAlignment(Qt::AlignHCenter);
    lineEdit_passsword->setFont(font);

    lineEdit_passsword_confirm = new QLineEdit();
    lineEdit_passsword_confirm->setMinimumHeight(30);
    lineEdit_passsword_confirm->setMaximumWidth(300);
    lineEdit_passsword_confirm->setAlignment(Qt::AlignHCenter);
    lineEdit_passsword_confirm->setFont(font);

    QLabel * lab_username =new QLabel();
    lab_username->setText("    账号:");

    QLabel * lab_passsword =new QLabel();
    lab_passsword->setText("    密码:");

    QLabel * lab_passsword2 =new QLabel();
    lab_passsword2->setText("确认密码:");

    QHBoxLayout *hlayout1 = new QHBoxLayout();
    QHBoxLayout *hlayout2 = new QHBoxLayout();
    QHBoxLayout *hlayout3 = new QHBoxLayout();
    QHBoxLayout *hlayout4 = new QHBoxLayout();

    QVBoxLayout *vlayout = new QVBoxLayout(this);

    hlayout1->addStretch(1);
    hlayout1->addWidget(lab_username);
    hlayout1->addWidget(lineEdit_username);
    hlayout1->addStretch(1);

    hlayout2->addStretch(1);
    hlayout2->addWidget(lab_passsword);
    hlayout2->addWidget(lineEdit_passsword);
    hlayout2->addStretch(1);

    hlayout3->addStretch(1);
    hlayout3->addWidget(lab_passsword2);
    hlayout3->addWidget(lineEdit_passsword_confirm);
    hlayout3->addStretch(1);

    hlayout4->addWidget(button_longin);
    hlayout4->addWidget(button_cancel);

    vlayout->addLayout(hlayout1);
    vlayout->addLayout(hlayout2);
    vlayout->addLayout(hlayout3);
    vlayout->addLayout(hlayout4);

    //调用套接字成员函数，连接服务器
    connect(button_longin, &QPushButton::clicked,this,&RegisterDialog::login);
}

void RegisterDialog::login()
{
    QString username = lineEdit_username->text();
    QString password = lineEdit_passsword->text();
    QString password2 = lineEdit_passsword_confirm->text();
    if(username.isEmpty()||password.isEmpty()||password2.isEmpty()){
        QMessageBox::information(this, "错误", "账号或密码为空！");
        return ;
    }

    if(password!=password2){
        QMessageBox::information(this, "错误", "两次密码不一致~");
        return ;
    }
    //调用主窗口传入的套接字成员函数，连接服务器
    m_clientTcpSocket->sendTLV(TASK_SIGNUP_SECTION1,username.size(),username.toUtf8().data());
}

void RegisterDialog::handelSignupPacket(const Packet &packet)
{

    qDebug()<<"注册界面处理接受数据";
    int type = packet._type;
    qDebug()<<"数据类型"<<packet._type;
    QString info;
    switch (type){
    case TASK_SIGNUP_SECTION1_RESP_OK://用户存在，则发送密文
    {
        QByteArray train = getTrainMessage(lineEdit_username->text(),packet._msg);//创建一个小火车，int + 用户名 + int + 加密密文
        m_clientTcpSocket->sendTLV(TASK_LOGIN_SECTION2,train.length(),train.data());//发送加密密文
        return;
    }
    case TASK_SIGNUP_SECTION1_RESP_ERROR:
        info = "账号已存在";
        break;
    case TASK_SIGNUP_SECTION2_RESP_OK:
        info = "注册成功";
        break;
    case TASK_SIGNUP_SECTION2_RESP_ERROR:
        info = "注册失败";
        break;
    default://如果不属于以上消息，说明不是该窗口需要接受的消息
        return;
    }
    QMessageBox::information(this, "提示", info);

}
